#include "database.h"
#include <QDebug>

Database* Database::database = 0;

Database::Database() : QObject(NULL)
{
    lib.load_file("library.xml");
}

Database* Database::get()
{
    if(database == NULL)
        database = new Database;
    return database;
}

QList<QString> Database::getPlaylists()
{
    QList<QString> playlists;

    pugi::xml_node playlist = lib.child("playlist");
    while(playlist)
    {
        playlists.append(playlist.attribute("name").as_string());
        playlist = playlist.next_sibling();
    }

    return playlists;
}

Playlist Database::getPlaylist(QString name)
{
    QList<VideoInfo> videos;
    pugi::xml_node playlist = lib.find_child_by_attribute("name", name.toStdString().c_str());
    for(pugi::xml_node video = playlist.child("song"); video; video = video.next_sibling())
    {
        VideoInfo vid;
        vid.id = video.attribute("id").as_string();
        vid.artist = video.attribute("artist").as_string();
        vid.title = video.attribute("title").as_string();
        vid.duration = video.attribute("duration").as_int();
        vid.viewCount = video.attribute("viewCount").as_int();
        vid.playlist = video.attribute("playlist").as_string();
        videos.append(vid);
    }
    qDebug() << "filled playlist";
    return Playlist(name, videos);
}

void Database::changeVideoAttribute(QString playlist, QString id, QString attributeName, QString attributeValue)
{
    qDebug("changing attribute!");
    qDebug() << playlist << id << attributeName << attributeValue;

    pugi::xml_node playlistnode = lib.find_child_by_attribute("name", playlist.toStdString().c_str());
    pugi::xml_node song = playlistnode.find_child_by_attribute("id", id.toStdString().c_str());

    song.attribute(attributeName.toStdString().c_str()).set_value(attributeValue.toStdString().c_str());
    lib.save_file("library.xml");
}

void Database::deleteSongFromPlaylist(VideoInfo song)
{
    qDebug("deleting song");
    pugi::xml_node playlist = lib.find_child_by_attribute("name", song.playlist.toStdString().c_str());
    playlist.remove_child(playlist.find_child_by_attribute("id", song.id.toStdString().c_str()));
    lib.save_file("library.xml");
}

void Database::addNewPlaylist(QString name)
{
    qDebug() << "adding playlist" << name.toAscii();
    pugi::xml_node playlist = lib.append_child("playlist");
    playlist.append_attribute("name") = name.toStdString().c_str();
    lib.save_file("library.xml");
}

void Database::addNewSong(QString playlistname, VideoInfo vid)
{
    vid.playlist = playlistname;
    pugi::xml_node playlist = lib.find_child_by_attribute("name", playlistname.toStdString().c_str());
    pugi::xml_node song = playlist.append_child("song");
    song.append_attribute("title") = vid.title.toStdString().c_str();
    song.append_attribute("artist") = vid.artist.toStdString().c_str();
    song.append_attribute("id") = vid.id.toStdString().c_str();
    song.append_attribute("duration") = int(vid.duration);
    song.append_attribute("viewCount") = int(vid.viewCount);
    song.append_attribute("playlist") = vid.playlist.toStdString().c_str();
    lib.save_file("library.xml");
}
